<?PHP
  $template = new Template();
  $template->load("site_edit");
  $page = new Page();
  $page->loadProperties($GLOBALS['db']->EscapeString($_GET['site']));
  if($_POST['save']){
	$oldpage = $page;
    $page->title = $_POST['title'];
    $page->menu = $_POST['menu'];
    $page->meta->description = $_POST['meta-description'];
    $page->meta->keywords = $_POST['meta-keywords'];
    $page->meta->robots = $_POST['meta-robots'];
	$page->alias = $_POST['alias'];
    $page->save();
	$page->editor->save($page,$oldpage);
	Role::clearAccess($page);
	if($_POST['rights']){
	  foreach($_POST['rights'] as $right){
	    $role = new Role();
		$role->load($right);
		$role->allowAccess($page);
	  }
	}
  }
  else if(isset($_POST['menu'])){
	$page->title = $_POST['title'];
	$page->alias = $_POST['alias'];
	$page->menu = $_POST['menu'];
	$page->editor->content = $_POST['content'];
  }
  $template->assign_var("PATH","/admin/index.php?page=site-edit&site=".$page->alias);
  $template->assign_var("ALIAS",$page->alias);

  if(!isset($_POST['menu'])){
    foreach(Role::getAllRolls() as $role){
      $index = $template->add_loop_item("RIGHTS");
      $template->assign_loop_var("RIGHTS", $index, "LABEL","/");
	  $template->assign_loop_var("RIGHTS", $index, "VALUE",$role->ID);
	  $template->assign_loop_var("RIGHTS", $index, "NAME",$role->name);
      if($role->canAccess($page)){
	    $template->assign_loop_var("RIGHTS", $index, "SELECTED", "selected=\"1\" ");
      }
	  else{
	    $template->assign_loop_var("RIGHTS", $index, "SELECTED", "");
	  }
    }
  }
  else{
    foreach(Role::getAllRolls() as $role){
      $index = $template->add_loop_item("RIGHTS");
      $template->assign_loop_var("RIGHTS", $index, "LABEL","/");
	  $template->assign_loop_var("RIGHTS", $index, "VALUE",$role->ID);
	  $template->assign_loop_var("RIGHTS", $index, "NAME",$role->name);
      if(in_array($role->ID,$_POST['rights'])){
	    $template->assign_loop_var("RIGHTS", $index, "SELECTED", "selected=\"1\" ");
	  }
      else{
	    $template->assign_loop_var("RIGHTS", $index, "SELECTED", "");
      }
    }
  }
  $index = $template->add_loop_item("MENU");
  $template->assign_loop_var("MENU", $index, "VALUE","0");
  $template->assign_loop_var("MENU", $index, "NAME","-- Kein Men&uuml; --");
  foreach(sys::getMenues() as $menu){
    $index = $template->add_loop_item("MENU");
    $template->assign_loop_var("MENU", $index, "VALUE",$menu->id);
    $template->assign_loop_var("MENU", $index, "NAME",$menu->name);
	if($_POST['menu'] == $menue->id){
      $template->assign_loop_var("MENU", $index, "SELECTED", "selected=\"1\" ");
	}
	else if($page->menu == $menue->id && !$_POST['menu']){
      $template->assign_loop_var("MENU", $index, "SELECTED", "");
    }
  }
  if($_POST['menu']){
    $template->assign_var("MENUPREVIEW",Menu::getCode($_POST['menu'],"<ul>","</ul>","<li>","</li>",""));
  }
  else if($page->menu > 0){
    $template->assign_var("MENUPREVIEW",Menu::getCode($page->menu,"<ul>","</ul>","<li>","</li>",""));
  }
  else{
    $template->assign_var("MENUPREVIEW","");
  }
  $template->assign_var("METADESCRIPTION",$page->meta->description);
  $template->assign_var("METAKEYWORDS",$page->meta->keywords);
  $template->assign_var("METAROBOTS",$page->meta->robots);
  $template->assign_var("TITLE",$page->title);
  $template->assign_var("EDITOR",$page->editor->getEditableCode());
  $template->output();
?>